﻿using DevExpress.XtraTreeList.Nodes;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using TestPrint.PrintNameHelp;

namespace TestPrint.CommonPrintSet
{
    public partial class frmSettingManager : Form
    {
        private BaseSetting m_BaseSetting = null;
        private Dictionary<Type, BaseSetting> controls = new Dictionary<Type, BaseSetting>();
        private string m_strKey;
        private readonly List<TreeItem> _settingList;
        public frmSettingManager()
        {
            InitializeComponent();
        }
        public frmSettingManager(List<TreeItem> settingList) : this()
        {
            _settingList = settingList;
        }
        public frmSettingManager(string strKey, List<TreeItem> settingList) : this(settingList)
        {
            m_strKey = strKey;
        }

        public string TipMessage { get; set; }

        private void frmLocalSettingManager_Load(object sender, EventArgs e)
        {
            CreateTree();
            tvMenu.ExpandAll();
            if (!string.IsNullOrEmpty(this.m_strKey))
            {
                foreach (object obj in this.tvMenu.Nodes)
                {
                    TreeListNode tnNode = (TreeListNode)obj;
                    this.SetSelectMenu(tnNode, this.m_strKey);
                }
            }

            if (!string.IsNullOrWhiteSpace(TipMessage))
                alertControl1.Show(this, null, TipMessage);
        }

        private bool SetSelectMenu(TreeListNode tnNode, string strKey)
        {
            bool result = false;
            if (strKey == null)
            {
                result = false;
            }
            else
            {
                TreeItem settingType = tvMenu.GetDataRecordByNode(tnNode) as TreeItem;
                if (settingType != null && strKey.Equals(settingType.Key))
                {
                    this.tvMenu.FocusedNode = tnNode;
                    result = true;
                }
                if (tnNode.HasChildren)
                {
                    foreach (object obj in tnNode.Nodes)
                    {
                        TreeListNode tnNode2 = (TreeListNode)obj;
                        if (this.SetSelectMenu(tnNode2, strKey))
                        {
                            return true;
                        }
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// 创建树形菜单
        /// </summary>
        private void CreateTree()
        {
            this.tvMenu.Nodes.Clear();
            List<TreeItem> settingList = _settingList;
            //List<SettingType> list = (from p in settingList
            //                          where string.IsNullOrEmpty(p.ParentKey)
            //                          select p).ToList<SettingType>();
            this.tvMenu.DataSource = settingList;
        }

        private void tvMenu_AfterFocusNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
        {
            btnApp.Enabled = true;
            if (this.m_BaseSetting != null)
                m_BaseSetting.Visible = false;

            TreeItem settingType = tvMenu.GetDataRecordByNode(e.Node) as TreeItem;
            if (settingType == null || settingType.ControlType == null) return;
            if (controls.ContainsKey(settingType.ControlType))
            {
                if (!string.IsNullOrEmpty(settingType.TreeNodeName))
                {
                    this.Text = settingType.TreeNodeName.Replace(@"\", "—");
                }
                else
                {
                    this.Text = "系统设置";
                }
                m_BaseSetting = controls[settingType.ControlType];
                m_BaseSetting.Visible = true;
            }
            else
            {
                this.m_BaseSetting = (BaseSetting)Activator.CreateInstance(settingType.ControlType);
                if (this.m_BaseSetting != null)
                {
                    if (!string.IsNullOrEmpty(settingType.TreeNodeName))
                    {
                        this.Text = settingType.TreeNodeName.Replace(@"\", "—");
                    }
                    else
                    {
                        this.Text = "系统设置";
                    }
                    this.m_BaseSetting.Dock = DockStyle.Fill;
                    panelControl1.Controls.Add(this.m_BaseSetting);
                    this.m_BaseSetting.Show();
                    controls.Add(settingType.ControlType, m_BaseSetting);
                    this.btnSave.Enabled = true;
                }
                else
                {
                    this.Text = "系统设置";
                    this.btnSave.Enabled = false;
                }
            }

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            foreach (KeyValuePair<Type, BaseSetting> pare in controls)
            {
                pare.Value.Save();
            }
            DialogResult = DialogResult.OK;
            this.Close();
        }


        private void btnApp_Click(object sender, EventArgs e)
        {
            foreach (KeyValuePair<Type, BaseSetting> pare in controls)
            {
                pare.Value.Save();
            }
            btnApp.Enabled = false;
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            this.Close();
        }

    }
}
